package com.hxc.shop.service;

import com.hxc.shop.bean.ShopMainImage;
import com.hxc.shop.common.ResultCode;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.mapper.Mysql.MainImageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MainImageService {
    @Resource
    private MainImageMapper mainImageMapper;

    public ResultVO getMainImage() {
        Integer count = 5;
        List<ShopMainImage> shopMainImageList = new ArrayList<>();
        try {
            shopMainImageList = mainImageMapper.getMainImageList(count);
        }catch (Exception e) {
            log.error("获取首页轮播图数据失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopMainImageList);
    }
    public ResultVO getAllMainImage() {
        List<ShopMainImage> shopMainImageList = new ArrayList<>();
        try {
            shopMainImageList = mainImageMapper.getAllMainImage();
        }catch (Exception e) {
            log.error("获取首页轮播图数据失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopMainImageList);
    }

    public ResultVO getMainImageByCount(Integer count) {
        List<ShopMainImage> shopMainImageList = new ArrayList<>();
        try {
            shopMainImageList = mainImageMapper.getMainImageList(count);
        }catch (Exception e) {
            log.error("获取首页轮播图数据失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopMainImageList);
    }

    public ResultVO insertMainImage(String mainImage) {
        if (StringUtils.isEmpty(mainImage)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"轮播图图片路径数据为空!");
        }
        ShopMainImage image = new ShopMainImage();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        image.setCreateTime(sdf.format(new Date()));
        image.setUpdateTime(sdf.format(new Date()));
        image.setMainImage(mainImage);

        try {
            mainImageMapper.insertMainImage(image);
        }catch (Exception e) {
            log.error("插入首页轮播图数据失败:"+image+"=="+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"插入首页轮播图数据成功!路径:"+image.getMainImage());
    }

    public ResultVO updateMainImage(Integer id,String mainImage) {
        if (id == 0 || id ==null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到需要修改的首页轮播图编号,s=无法修改!");
        }
        if (StringUtils.isEmpty(mainImage)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"首页轮播图图片路径数据为空!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ShopMainImage shopMainImage = new ShopMainImage();
        shopMainImage.setUpdateTime(sdf.format(new Date()));
        shopMainImage.setMainImage(mainImage);
        shopMainImage.setId(id);
        try {
            mainImageMapper.updateMainImage(shopMainImage);
        }catch (Exception e) {
            log.error("更新首页轮播图数据失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"更新轮播图数据失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"更新成功!");
    }

    public ResultVO deleteMainImage(Integer id) {
        ShopMainImage shopMainImage = new ShopMainImage();
        try {
            shopMainImage = mainImageMapper.getMainImageById(id);
        }catch (Exception e){
            log.error("查找轮播图数据失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找轮播图数据失败:"+e.getMessage());
        }
        if (shopMainImage == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未查找到编号:"+id+"的轮播图数据!");
        }
        try {
            mainImageMapper.deleteMainImageById(id);
        }catch (Exception e) {
            log.error("删除轮播图数据失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"删除轮播图数据失败:"+e.getMessage());
        }

        return new ResultVO(ResultCode.SUCCESS.getCode(), "删除轮播图数据成功!轮播图编号:"+id);
    }

    public ResultVO getMainImageById(Integer id) {
        ShopMainImage shopMainImage = new ShopMainImage();
        try {
            shopMainImage = mainImageMapper.getMainImageById(id);
        }catch (Exception e) {
            log.error("获取首页轮播图数据失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopMainImage);
    }
}
